package kr.co.choongang;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;


@WebServlet("/Upload")
@MultipartConfig(maxFileSize = 1024*1024*2, location = "c:\\upload")
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

   
    private String getFilename(Part part){
    	String fileName = null;
    	String contentDispositonHeader = part.getHeader("content-disposition");
    	String[] elements = contentDispositonHeader.split(";");
    	for(String element:elements){
    		if(element.trim().startsWith("filename")){
    			fileName = element.substring(element.indexOf('=')+1);
    			fileName = fileName.trim().replace("\"", "");
    		}
    	}
    	return fileName;
    }	//end getFilename

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("euc-kr");
		
		Part part = request.getPart("theFile");
		String fileName = getFilename(part);
		if(fileName!=null&&!fileName.isEmpty()){
			
			part.write(fileName);
		}
		
		String author = request.getParameter("theAuthor");
		author = new String(author.getBytes("iso-8859-1"),"euc-kr");
		response.setContentType("text/html;charset=euc-kr");
		PrintWriter out = response.getWriter();
		
		out.print("작성자:"+author+"<br>");
		out.print("파일명:<a href='FileDown?file_name="+fileName+"'>"+fileName+"</a><br>");
		out.print("파일크기:"+part.getSize()+"<br>");
	}

}
